package com.example.hasee.zhcd.Activities.ChuanWu;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;


/**
 * Created by hasee on 2017/10/13.
 */

public class LPInnerActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private TextView place;
    private TextView shipname;
    private TextView shiporder;
    private TextView entruster;
    private TextView pztons;
    private TextView qstons;
    private TextView starttime;
    private TextView finishtime;
    private TextView leavetime;
    private TextView zx;
    private TextView hl;
    private TextView lg;
    private TextView lx;
    private TextView kbys;
    private TextView kbtl;
    private TextView lgys;
    private TextView lgtl;
    private TextView cbzgdt;
    private TextView yibo;
    private ImageView pic;
    private String picString;
    private Bitmap bitmap;
    private String strShipYearNum;

    @Override
    public int contentViewId() {
        return R.layout.activity_lp_inner;
    }

    @Override
    public void set() {
        strShipYearNum = getIntent().getExtras().getString("fshipyearnumvcr");
        title.setText("离港船舶");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        pic.setOnClickListener(this);
        cbzgdt.setOnClickListener(this);
        getLeavePortDetail();
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        place = (TextView) findViewById(R.id.place);
        shipname = (TextView) findViewById(R.id.shipname);
        shiporder = (TextView) findViewById(R.id.shiporder);
        entruster = (TextView) findViewById(R.id.entruster);
        pztons = (TextView) findViewById(R.id.pztons);
        qstons = (TextView) findViewById(R.id.qstons);
        starttime = (TextView) findViewById(R.id.starttime);
        finishtime = (TextView) findViewById(R.id.finishtime);
        leavetime = (TextView) findViewById(R.id.leavetime);
        pic = (ImageView) findViewById(R.id.pic);
        zx = (TextView) findViewById(R.id.zx);
        hl = (TextView) findViewById(R.id.hl);
        lg = (TextView) findViewById(R.id.lg);
        lx = (TextView) findViewById(R.id.lx);
        kbys = (TextView) findViewById(R.id.kbys);
        kbtl = (TextView) findViewById(R.id.kbtl);
        lgys = (TextView) findViewById(R.id.lgys);
        lgtl = (TextView) findViewById(R.id.lgtl);
        cbzgdt = (TextView) findViewById(R.id.cbzgdt);
        yibo = (TextView) findViewById(R.id.yibo);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(LPInnerActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "YWCX_lgcbDetail":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    JSONObject data = json.getJSONObject("data");
                    place.setText(data.getString("fberth"));
                    shipname.setText(data.getString("fshipname"));
                    shiporder.setText(data.getString("fshipordernum"));
                    entruster.setText(data.getString("fwtf"));
                    pztons.setText(data.getString("fpzton"));
                    qstons.setText(data.getString("fqston"));
                    starttime.setText(data.getString("fkgsj"));
                    finishtime.setText(data.getString("fwgsj"));
                    leavetime.setText(data.getString("flgsj"));
                    zx.setText(data.getString("fzx"));
                    hl.setText(data.getString("fhl"));
                    lg.setText(data.getString("flg"));
                    lx.setText(data.getString("flx"));
                    kbys.setText(data.getString("fkbys"));
                    kbtl.setText(data.getString("fkbtl"));
                    lgys.setText(data.getString("flgys"));
                    lgtl.setText(data.getString("flgtl"));
                    yibo.setText(data.getString("fyb"));
                    picString = data.getString("fscandoc");
                    if ("".equals(picString)) {
                        pic.setClickable(false);
                    } else {
                        bitmap = base64ToBitmap(data.getString("fscandoc"));
                        pic.setImageBitmap(bitmap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getLeavePortDetail() {
        ProgressDialogUtils.createProgressDialog(LPInnerActivity.this, "正在获取数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strShipYearNum", strShipYearNum);
        httpUtils.get("YWCX_lgcbDetail", map, "YWCX_lgcbDetail");
    }


    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    private void IceoundDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(LPInnerActivity.this);
        builder.setMessage("是否生成封航记录？");
        builder.setTitle("提示");
        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                arg0.dismiss();
                Intent intent = new Intent(LPInnerActivity.this, MakePicActivity.class);
                intent.putExtra("fshipyearnumvcr", strShipYearNum);
                intent.putExtra("bol", true);
                startActivity(intent);
            }
        });
        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(LPInnerActivity.this, MakePicActivity.class);
                intent.putExtra("fshipyearnumvcr", strShipYearNum);
                intent.putExtra("bol", false);
                startActivity(intent);
            }
        });
        builder.create().show();
    }

    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }
        // 在低版本中用一行的字节x高度
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                GlobalParameter.JumpFrontPage(getApplicationContext());
                break;
            case R.id.pic:
                Intent intent = new Intent(LPInnerActivity.this, PictrueActivity.class);
                intent.putExtra("string", picString);
                startActivity(intent);
                break;
            case R.id.cbzgdt:
                IceoundDialog();
                break;
        }
    }
}
